﻿using System.Collections.Generic;
using MicropleSoft.WmsServiceLibrary.Domain;
using MicropleSoft.WmsServiceLibrary.ServiceFactory;

namespace MicropleSoft.WmsServiceLibrary.Menu
{
    public class MenuBuilder : IMenuBuilder
    {
        private readonly IMenuListService _menuListService =   
            ServiceFactoryCreator.CreateAbstractServiceFactory().CreateMenuListService();
        public List<MenuItemModel> GetRootItems()
        {
            var resultList = new List<MenuItemModel>();
            var rootList = _menuListService.Query(" and PARENT_GROUP_GID = -1 and LANG='zh' and COUNTRY='cn' ");
            foreach (var menuList in rootList)
            {
                var model = new MenuItemModel
                                {
                                    Caption = menuList.Caption,
                                    Url = menuList.Url,
                                    Childrens = new List<MenuItemModel>(),
                                    MenuListEntity = menuList
                                };
                BuildMenuItemModel(model);
                resultList.Add(model);
            }
            return resultList;
        }

        private void BuildMenuItemModel(MenuItemModel menuItemModel)
        {
            var list = _menuListService.Query(string.Format(" and PARENT_GROUP_GID = {0} and LANG='zh' and COUNTRY='cn' ",
                menuItemModel.MenuListEntity.GroupId));
            foreach (var menuList in list)
            {
                var model = new MenuItemModel
                {
                    Caption = menuList.Caption,
                    Url = menuList.Url,
                    Childrens = new List<MenuItemModel>(),
                    MenuListEntity = menuList
                };
                BuildMenuItemModel(model);
                menuItemModel.Childrens.Add(model);
            }
        }
    }
}
